//
//  CPSHDraw.m
//  CardMoveGame
//
//  Created by peac on 2025/11/7.
//

#import "CPSHDrawViewController.h"

@interface CPSHDrawViewController ()
@property(nonatomic,strong)UIButton *cjqcountBtn;

@end

@implementation CPSHDrawViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIImageView *drawbgIv=[[UIImageView alloc]initWithFrame:self.view.bounds];
    drawbgIv.contentMode = UIViewContentModeScaleAspectFill;
    drawbgIv.image=[UIImage imageNamed:@"bjsummon"];
    [self.view addSubview:drawbgIv];
    
    UIImageView* charactor = [[UIImageView alloc] init];
    charactor.frame = CGRectMake(60, self.view.frame.size.height*0.2, self.view.frame.size.width-120, self.view.frame.size.height*0.430);
    charactor.image = [UIImage imageNamed:@"人物summon"];
    charactor.contentMode = UIViewContentModeScaleAspectFill;
    [self.view addSubview:charactor];
    
    UIButton *mdsummonx1Btn=[UIButton buttonWithType:UIButtonTypeCustom];
    
    mdsummonx1Btn.frame=CGRectMake(self.view.frame.size.width/2-20-self.view.frame.size.height*0.065*2.688, self.view.frame.size.height*0.7, self.view.frame.size.height*0.065*2.688, self.view.frame.size.height*0.065);
    [mdsummonx1Btn setBackgroundImage:[UIImage imageNamed:@"召唤1次"] forState:UIControlStateNormal];
    [mdsummonx1Btn addTarget:self action:@selector(mdsummonx1Btnclicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:mdsummonx1Btn];
    
    UIButton *mdsummonx10Btn=[UIButton buttonWithType:UIButtonTypeCustom];
    
    mdsummonx10Btn.frame=CGRectMake(self.view.frame.size.width/2+20, self.view.frame.size.height*0.7, self.view.frame.size.height*0.065*2.688, self.view.frame.size.height*0.065);
    [mdsummonx10Btn setBackgroundImage:[UIImage imageNamed:@"召唤10次-"] forState:UIControlStateNormal];
    [mdsummonx10Btn addTarget:self action:@selector(mdsummonx10Btnclicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:mdsummonx10Btn];
    
    self.cjqcountBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    self.cjqcountBtn.frame=CGRectMake(self.view.frame.size.width-self.view.frame.size.height*0.059*2, self.view.frame.size.height*0.5, self.view.frame.size.height*0.059*2, self.view.frame.size.height*0.059);
    [self.cjqcountBtn setBackgroundImage:[UIImage imageNamed:@"mdchoujiangquanbg"] forState:UIControlStateNormal];
    [self.cjqcountBtn setTitle:[NSString stringWithFormat:@"%ld",(long)[[NSUserDefaults standardUserDefaults] integerForKey:@"cjqcount"]] forState:UIControlStateNormal];
    [self.view addSubview:self.cjqcountBtn];
    self.cjqcountBtn.titleEdgeInsets=UIEdgeInsetsMake(0,20,5,0);
}
-(void)mdsummonx1Btnclicked{
    if ([[NSUserDefaults standardUserDefaults] integerForKey:@"cjqcount"]<1) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Tips"
                                                                     message:@"Not enough item"
                                                              preferredStyle:UIAlertControllerStyleAlert];
    
        UIAlertAction *restartAction = [UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
          
        }];
    
        [alert addAction:restartAction];
        [self presentViewController:alert animated:YES completion:nil];
        
        return;
    }
    [[NSUserDefaults standardUserDefaults] setInteger:[[NSUserDefaults standardUserDefaults] integerForKey:@"cjqcount"]-1 forKey:@"cjqcount"];
    [self.cjqcountBtn setTitle:[NSString stringWithFormat:@"%ld",(long)[[NSUserDefaults standardUserDefaults] integerForKey:@"cjqcount"]] forState:UIControlStateNormal];
    UIView *showView=[[UIView alloc]initWithFrame:self.view.frame];
    showView.backgroundColor=[UIColor colorWithWhite:0.0 alpha:0.6];
    [self.view addSubview:showView];
    UIImageView *showImageView=[[UIImageView alloc]initWithFrame:self.view.frame];
    showImageView.image=[UIImage imageNamed:@"mdsummonbgimage"];
    [showView addSubview:showImageView];
    UIImageView *itemIView=[[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2-self.view.frame.size.height*0.118/2,self.view.frame.size.height*0.45, self.view.frame.size.height*0.118, self.view.frame.size.height*0.118)];
    itemIView.image=[UIImage imageNamed:[NSString stringWithFormat:@"mditemimage%ld",(long)[self getonedrawprize]]];
    [showView addSubview:itemIView];
    [self.view addSubview:showView];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [showView removeFromSuperview];
    });
    
}
-(void)mdsummonx10Btnclicked{
    
    if ([[NSUserDefaults standardUserDefaults] integerForKey:@"cjqcount"]<1) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Tips"
                                                                     message:@"Not enough items"
                                                              preferredStyle:UIAlertControllerStyleAlert];
    
        UIAlertAction *restartAction = [UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
          
        }];
    
        [alert addAction:restartAction];
        [self presentViewController:alert animated:YES completion:nil];
        
        
        return;
    }
    [[NSUserDefaults standardUserDefaults] setInteger:[[NSUserDefaults standardUserDefaults] integerForKey:@"cjqcount"]-10 forKey:@"cjqcount"];
    [self.cjqcountBtn setTitle:[NSString stringWithFormat:@"%ld",(long)[[NSUserDefaults standardUserDefaults] integerForKey:@"cjqcount"]] forState:UIControlStateNormal];
    UIView *showView=[[UIView alloc]initWithFrame:self.view.frame];
    showView.backgroundColor=[UIColor colorWithWhite:0.0 alpha:0.6];
    showView.tag=101;
    [self.view addSubview:showView];
    UIImageView *showImageView=[[UIImageView alloc]initWithFrame:self.view.frame];
    showImageView.image=[UIImage imageNamed:@"mdsummonbgimage"];
    [showView addSubview:showImageView];
    for (int i = 0; i <10; i++) {
    
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(i * 0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
       
            CGFloat itemWidth = (self.view.bounds.size.width - 40) / 4;
            CGFloat itemHeight = itemWidth/1.06;
            int row = i / 4;
            int col = i % 4;
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10 + col * (itemWidth + 5),
                                                                                   self.view.frame.size.height*0.2+self.view.frame.size.height/7+10 + row * (itemHeight + 10),
                                                                                   itemWidth, itemHeight)];
            imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"mditemimage%ld",(long)[self getonedrawprize]]];
            [showView addSubview:imageView];
            
   
            
            // 添加动画效果
            imageView.transform = CGAffineTransformMakeScale(0.1, 0.1);
            [UIView animateWithDuration:0.5 animations:^{
                imageView.transform = CGAffineTransformIdentity;
            }];
        });
    }
  UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]
                                        initWithTarget:self
                                        action:@selector(mdclosefailview)];
  [showView addGestureRecognizer:tapGesture];
    
}
-(void)mdclosefailview{
    UIView *view=(UIView*)[self.view viewWithTag:101];
    [view removeFromSuperview];
    
}


-(NSInteger)getonedrawprize{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    int randomIndex = arc4random_uniform(100);
    int index=0;
    if (randomIndex<40) {
        index=1;
        [defaults setInteger:[defaults integerForKey:@"mdgold"]+100 forKey:@"mdgold"];
    }else if(randomIndex<44){
        index=2;
        [defaults setInteger:[defaults integerForKey:@"mdgold"]+500 forKey:@"mdgold"];
    }else if(randomIndex<84){
        index=3;
        [defaults setInteger:[defaults integerForKey:@"mdjingyan"]+1000 forKey:@"mdjingyan"];
    }else if(randomIndex<88){
        index=4;
        [defaults setInteger:[defaults integerForKey:@"mdjingyan"]+5000 forKey:@"mdjingyan"];
    }else if(randomIndex<92){
        index=5;
    
    }else if(randomIndex<96){
        index=6;
      
    }else if(randomIndex<100){
        index=7;
      
    }
    NSLog(@"-----%d",index);
    
    return index;
    
    
}
@end
